<?php
/**
 * Created by PhpStorm.
 * User: user1
 * Date: 2018/5/21
 * Time: 13:38
 */

namespace app\admin\controller;
use app\api\model\Activity as ActivityModel;
use app\api\validate\ActivityValidate;

class Activity extends Common
{
    /**
     * 活动列表
     * @return \think\response\Json|\think\response\View
     */
    public function index() {
        if (request()->isAjax()) {
            $data = ActivityModel::getList()->toArray();
            return admin_return_data('活动列表', $data['total'], $data['data']);
        }
        return view();
    }

    /**
     * 创建或者编辑品牌
     * @return \think\response\Json|\think\response\View
     * @throws \app\api\exception\ParameterException
     * @throws \think\exception\DbException
     */
    public function save_activity() {
        $id = input('param.id', 0);
        if (request()->isAjax()) {
            $activity_validate = new ActivityValidate();
            $activity_validate->goCheck();
            $param = $activity_validate->getDataByRule(input('param.'));
            $data = ActivityModel::createOrUpdateActivity($param, $id);
            return json($data);
        } else {
            $data = ['start_time_date' => '', 'end_time_date' => ''];
            if ($id > 0) {
                $data = ActivityModel::get($id);
            }
            $this->assign('data', $data);

            return view();
        }
    }
}